package com.yuandian.management.controller;


import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.management.service.IAgentThresholdService;
import com.yuandian.management.vo.AgentConfigManager;
import com.yuandian.management.vo.AgentThresholdPojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * </p>
 *
 * agent阀值表 前端控制器
 * @author fucx
 * @since 2021-01-27
 */
@RestController
@RequestMapping("/modules/monitor/agent/threshold")
@Api(tags = "agent状态监控-阈值管理")
public class AgentThresholdController {
	
	@Autowired
    private IAgentThresholdService agentThresholdService;


    /**
     * 更新agent阀值
     * 
     * @param threshold
     * @return
     */
	@PostMapping("/updateThresholdByCode")
	@ApiOperation(value = "更新agent阀值", notes = "更新agent阀值")
    public R updateThresholdByCode(@RequestBody AgentThresholdPojo threshold) {
        threshold.setUpdateBy(SecurityUtils.getUser().getName());
        threshold.setUpdateTime(new Date());
        int num = agentThresholdService.updateByCodeSelective(threshold);
        if (num == 0) {
            return R.failed(null,"更新失败！");
        }
		// 更新缓存里面的阀值
		Map<String, AgentThresholdPojo> agentThresholdMap = AgentConfigManager.agentThresholdMap;

		// 这里查出来，是同时更新采集频率，因为采集频率不在前端更新，只能在数据库里面更新
		AgentThresholdPojo po = agentThresholdService.queryAgentThresholdByCode(threshold.getThresholdCode());
		if (po != null) {
			// 更新为最新的阀值
			agentThresholdMap.put(po.getThresholdCode(), po);
		}

        return R.ok();
    }

    /**
     * 查询agent阀值
     * 
     * @param
     * @return
     */
    @GetMapping("/queryAgentThresholdByCode")
	@ApiOperation(value = "查询agent阀值", notes = "查询agent阀值")
    public R queryAgentThresholdByCode() {
        AgentThresholdPojo threshold = agentThresholdService
                .queryAgentThresholdByCode(AgentConfigManager.AGENT_COLLECTION_THRESHOLD);
        return R.ok(threshold);
    }
}

